package com.sdgakj.interviews.job20180802;


/**
 * @Author: Du Ronghong
 * @Description:
 * @Date: Created in 18:49 2018/8/14
 * @Modified By:
 */
public class StringTest {

    public static void main(String[] args) {
        int num = 5;
        System.out.println(get(num));
        System.out.println(get2(num));

        for (int i = 1; i < 10; i++) {
            System.out.print(fib(i) + " ");
        }

        System.out.println();
        System.out.println("===========================");
        for (int i = 1; i < 10; i++) {
            System.out.print(fib2(i) + " ");
        }
    }

    private static int get(int num) {
        return num <= 1 ? num : num * get(num - 1);
    }

    private static int get2(int num) {
        if (num <= 1) {
            return num;
        }
        int sum = 1;
        for (int i = 1; i <= num; i++) {
            sum *= i;
        }
        return sum;
    }

    // 求斐波那契数列第n项
    private static int fib(int num) {
        return num <= 1 ? num : fib(num - 1) + fib(num - 2);
    }

    private static int fib2(int num) {
        if (num <= 1) {
            return num;
        }
        int one = 0;
        int two = 1;
        int temp = 0;

        for (int i = 2; i <= num; i++) {
            temp = one + two;
            one = two;
            two = temp;
        }
        return temp;
    }

}
